//
//  AuctionNewUserEnjoyView.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/2/24.
//  Copyright © 2020 sun. All rights reserved.
//

@interface CouponsView : UIView
@property (nonatomic, assign) CGFloat price;
@property (nonatomic, copy) void(^couponsViewButtonClickBlock)(void);
@end

@interface CouponsCell : UICollectionViewCell
@property (nonatomic, strong, readonly) UIImageView *imageView;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) CGFloat price;
@property (nonatomic, assign) CGFloat price1;
@end
#import "AuctionNewUserEnjoyView.h"
#import "AuctionHomeModel.h"

@interface AuctionNewUserEnjoyView() <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) CouponsView *headerView;
@property (nonatomic, copy) void(^enterNewUserEnjoyCouponBlock)(void);

@property (nonatomic, assign, setter=setHaveCoupon:, getter=isHaveCoupon) BOOL isHaveCoupon;
@end

@implementation AuctionNewUserEnjoyView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.layer.masksToBounds = true;
        self.layer.cornerRadius = 4.0;
        [self initBlock];
        [self initContentView];
    }
    return self;
}

- (void)initContentView {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"auction_header_new_coupon_bg_img")];
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(0);
        make.bottom.mas_offset(-10);
        make.width.mas_equalTo(self.headerView.mas_height).multipliedBy(100.0 / 167);
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerView.mas_right).mas_offset(0);
        make.top.mas_offset(10);
        make.bottom.right.mas_offset(-10);
    }];
}

#pragma mark -- mark 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//        layout.estimatedItemSize = CGSizeMake(100, 160);
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = false;
        _collectionView.showsHorizontalScrollIndicator = false;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.layer.masksToBounds = true;
        _collectionView.layer.cornerRadius = 4.0;
        [_collectionView registerClass:[CouponsCell class] forCellWithReuseIdentifier:@"CouponsCell_id"];
        [self addSubview:_collectionView];
    }
    return _collectionView;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    if (self.isHaveCoupon) {
        CGFloat item_w = (self.collectionView.width - 10 - 10 - 10) * 0.5;
        CGFloat item_h = self.collectionView.height - 10 - 10;
        layout.itemSize = CGSizeMake(item_w, item_h);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.minimumLineSpacing = 10;
    }else {
        if (self.dataModel.data.goods.count > 2) {
            // 三个数据
            layout.minimumLineSpacing = 10;
            CGFloat item_w = (self.collectionView.width - 10 - 10 - 10 - 10) / 3;
            CGFloat item_h = self.collectionView.height - 10 - 10;
            layout.itemSize = CGSizeMake(item_w, item_h);
            layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        }else {
            // 两条数据
            layout.minimumLineSpacing = 40;
            CGFloat item_w = (self.collectionView.width - 40 - 40 - 40) / 2;
            CGFloat item_h = self.collectionView.height - 10 - 10;
            layout.itemSize = CGSizeMake(item_w, item_h);
            layout.sectionInset = UIEdgeInsetsMake(10, 40, 10, 40);
        }
    }
    layout.minimumInteritemSpacing = 10;
    [self.collectionView setCollectionViewLayout:layout];
}
- (CouponsView *)headerView {
    if (!_headerView) {
        self.headerView = [[CouponsView alloc] initWithFrame:CGRectZero];
        _headerView.couponsViewButtonClickBlock = self.enterNewUserEnjoyCouponBlock;
        [self addSubview:_headerView];
    }
    return _headerView;
}

- (void)initBlock {
    SKWeakSelf;
    self.enterNewUserEnjoyCouponBlock = ^{
        if (weakSelf.couponUseButtonClickBlock) {
            weakSelf.couponUseButtonClickBlock();
        }
    };
}

- (void)reloadContentView {
    // 刷新布局
    if (self.isHaveCoupon) {
        self.headerView.hidden = false;
        [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.headerView.mas_right).mas_offset(0);
            make.top.mas_offset(10);
            make.bottom.right.mas_offset(-10);
        }];
    }else {
        self.headerView.hidden = true;
        [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_offset(10);
            make.bottom.right.mas_offset(-10);
        }];
    }
}
#pragma mark -- UICollectionViewDelegate UICollectionViewDataSource

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CouponsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CouponsCell_id" forIndexPath:indexPath];
    NSArray<ProductDetailModel *> *goods = self.dataModel.data.goods;
    ProductDetailModel *model = goods[indexPath.item];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:SKIMAGE_NAMED(@"cell_default_img")];
    cell.title = model.name;
    cell.price = [model.price floatValue];
    cell.price1 = [model.price floatValue];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSArray<ProductDetailModel *> *goods = self.dataModel.data.goods;
    return goods.count;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.couponProductButtonClickBlock) {
        self.couponProductButtonClickBlock(indexPath.item);
    }
}

- (void)setDataModel:(AuctionHomeModel *)dataModel {
    _dataModel = dataModel;
    AuctionHomeCouponModel *coupon = dataModel.data.coupon;
    if (coupon && coupon.ID.length > 0) {
        // 有优惠券
        self.isHaveCoupon = true;
        self.headerView.price = [coupon.money floatValue];
    }else {
        // 没有优惠券
        self.isHaveCoupon = false;
    }
    [self reloadContentView];
    [self.collectionView reloadData];
}
@end

#pragma mark -- mark 优惠券View
@interface CouponsView()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *couponLabel;
@property (nonatomic, strong) UIButton *button;
@end
@implementation CouponsView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initContentView];
    }
    return self;
}
- (void)initContentView {
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.left.mas_lessThanOrEqualTo(10);
        make.bottom.mas_equalTo(self.priceLabel.mas_top).mas_offset(-6);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.bottom.mas_equalTo(self.mas_centerY).mas_offset(0);
        make.left.mas_lessThanOrEqualTo(10);
    }];
    [self.couponLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.left.mas_lessThanOrEqualTo(10);
        make.centerY.mas_equalTo(self.mas_centerY).mas_offset(10);
    }];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.left.mas_offset(8.5);
        make.height.mas_equalTo(self.button.mas_width).multipliedBy(34.0/84);
        make.top.mas_equalTo(self.couponLabel.mas_bottom).mas_offset(10);
    }];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.button.layer.cornerRadius = self.button.height * 0.5;
}
#pragma mark -- mark 懒加载
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        self.titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = SKFONT_BOLD_SIZE(18);
        _titleLabel.text = @"新人专享";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}
- (UILabel *)priceLabel {
    if (!_priceLabel) {
        self.priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = SKColorWithHex(0xF5EBD6);
        _priceLabel.text = @"";
        _priceLabel.font = SKFONT_BOLD_SIZE(34);
        [self addSubview:_priceLabel];
    }
    return _priceLabel;
}

- (UILabel *)couponLabel {
    if (!_couponLabel) {
        self.couponLabel = [[UILabel alloc] init];
        _couponLabel.textColor = [UIColor whiteColor];
        _couponLabel.text = @"优惠券";
        _couponLabel.textAlignment = NSTextAlignmentCenter;
        _couponLabel.font = SKFONT_BOLD_SIZE(18);
        [self addSubview:_couponLabel];
    }
    return _couponLabel;
}
- (UIButton *)button {
    if (!_button) {
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button setTitle:@"去使用" forState:UIControlStateNormal];
        [_button setTitleColor:SKColorWithHex(0xA44E00) forState:UIControlStateNormal];
        _button.backgroundColor = SKColorWithHex(0xF6CAA3);
        _button.titleLabel.font = SKFONT_BOLD_SIZE(17);
        _button.layer.masksToBounds = true;
        [_button addTarget:self action:@selector(useButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_button];
    }
    return _button;
}
- (void)setPrice:(CGFloat)price {
    NSString *s = [NSString stringWithFormat:@"%.0f", price];
    NSString *priceS = [NSString stringWithFormat:@"¥%@", s];
    NSMutableAttributedString *mAttri = [[NSMutableAttributedString alloc] initWithString:priceS];
    NSShadow *shadow=[[NSShadow alloc]init];
    shadow.shadowBlurRadius = 1.0;
    shadow.shadowOffset = CGSizeMake(0, 0);
    shadow.shadowColor = SKColorWithHex(0xFF4B2F);
    [mAttri addAttributes:@{NSForegroundColorAttributeName:SKColorWithHex(0xF5EBD6), NSShadowAttributeName:shadow, NSFontAttributeName:SKFONT_BOLD_SIZE(18)} range:[priceS rangeOfString:@"¥"]];
    [mAttri addAttributes:@{NSForegroundColorAttributeName:SKColorWithHex(0xF5EBD6), NSFontAttributeName:SKFONT_BOLD_SIZE(34)} range:[priceS rangeOfString:s]];
    self.priceLabel.attributedText = mAttri;
}
#pragma mark -- mark EVENT

- (void)useButtonClick:(UIButton *)sender {
    if (self.couponsViewButtonClickBlock) {
        self.couponsViewButtonClickBlock();
    }
}
@end

#pragma mark -- mark CouponsCell
@interface CouponsCell()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *priceLabel1;
@end
@implementation CouponsCell
@synthesize imageView = _imageView;
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self initContentView];
    }
    return self;
}

- (void)initContentView {
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_offset(0);
        make.height.mas_equalTo(self.imageView.mas_width);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imageView.mas_bottom).mas_offset(4);
        make.bottom.mas_equalTo(self.priceLabel.mas_top).mas_offset(-4);
        make.left.right.mas_equalTo(self.imageView);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imageView);
        make.bottom.mas_offset(0);
        make.height.mas_equalTo(14);
    }];
    [self.priceLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.priceLabel.mas_right).mas_offset(4);
        make.right.mas_lessThanOrEqualTo(self.mas_right).mas_offset(0);
        make.bottom.mas_equalTo(self.priceLabel);
    }];
}

#pragma mark -- mark 懒加载
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.layer.masksToBounds = true;
        _imageView.layer.cornerRadius = 4.0f;
        [self addSubview:_imageView];
    }
    return _imageView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        self.titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = SKColorWithHex(0x333333);
        _titleLabel.font = SKFONT_BOLD_SIZE(13);
        _titleLabel.text = @"";
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}
- (UILabel *)priceLabel {
    if (!_priceLabel) {
        self.priceLabel = [[UILabel alloc] init];
        _priceLabel.text = @"";
        [self addSubview:_priceLabel];
    }
    return _priceLabel;
}
- (UILabel *)priceLabel1 {
    if (!_priceLabel1) {
        self.priceLabel1 = [[UILabel alloc] init];
        _priceLabel1.text = @"";
        [self addSubview:_priceLabel1];
    }
    return _priceLabel1;
}

- (void)setTitle:(NSString *)title {
    self.titleLabel.text = title;
    _title = title;
}
- (void)setPrice:(CGFloat)price {
    NSString *s = [NSString stringWithFormat:@"%.0f", price];
    NSString *priceS = [NSString stringWithFormat:@"¥%@", s];
    NSMutableAttributedString *mAttri = [[NSMutableAttributedString alloc] initWithString:priceS];
    [mAttri addAttributes:@{NSForegroundColorAttributeName:SKColorWithHex(0xFF3030), NSFontAttributeName:SKFONT_BOLD_SIZE(10)} range:[priceS rangeOfString:@"¥"]];
    [mAttri addAttributes:@{NSForegroundColorAttributeName:SKColorWithHex(0xFF3030), NSFontAttributeName:SKFONT_BOLD_SIZE(13)} range:[priceS rangeOfString:s]];
    self.priceLabel.attributedText = mAttri;
}
- (void)setPrice1:(CGFloat)price1 {
    NSString *s = [NSString stringWithFormat:@"¥%.0f", price1];
    NSAttributedString *attributeS = [[NSAttributedString alloc] initWithString:s attributes:@{NSForegroundColorAttributeName:SKColorWithHex(0xB1B1B1), NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSFontAttributeName:SKFONT_BOLD_SIZE(10)}];
    self.priceLabel1.attributedText = attributeS;
}
@end
